package org.jeecg.modules.paycenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.paycenter.enums.PayChannelEnum;
import org.jeecg.modules.wlhy.bill.entity.HyDriverBill;
import org.jeecg.modules.wlhy.bill.service.IHyDriverBillService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAccountBookService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 支付服务
 */
@RestController
@RequestMapping("/paycenter/hsyh")
public class HuiShangPayCenterController {
    @Autowired
    private IHyDriverBillService hyDriverBillService;
    @Autowired private UserUtil userUtil;

    @Resource
    private IHyCustomerAccountBookService customerAccountBookService;

    @Value(value="${jeecg.path.bank}")
    public String bankPath;

    //region 司机

    @GetMapping(value = "/driver/bill/getTotal")
    public Result<?> driverBillGetTotal() {
        HyDriver driver = userUtil.getDriver();
        Map<String, String> totalAmtByDriver = hyDriverBillService.getTotalAmtByDriverIdForHsyh(driver.getId() , PayChannelEnum.HUNDSUN.channelType);
        return Result.OK(totalAmtByDriver);
    }
    @GetMapping(value = "/driver/bill/list")
    public Result<?> driverBillList(HyDriverBill hyDriverBill,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        HyDriver driver = userUtil.getDriver();
        QueryWrapper<HyDriverBill> queryWrapper = QueryGenerator.initQueryWrapper(hyDriverBill, req.getParameterMap());
        queryWrapper.eq("driver_id", driver.getId())
                .eq("resp_source","0")
                .eq("pay_channel", PayChannelEnum.HUNDSUN.channelType)
                .ne("bill_type","3");
        Page<HyDriverBill> page = new Page<HyDriverBill>(pageNo, pageSize);
        IPage<HyDriverBill> pageList = hyDriverBillService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @GetMapping(value = "customer/getAcccount")
    public Result<?> getCustomerAccountInfo() {
        HyCustomer customer = userUtil.getCustomer();
        if (customer == null) {
            throw new JeecgBootException("未获取到货主信息");
        }
        //待结算金额
        BigDecimal djsAmt = customerAccountBookService.getDJSByCustomerId(customer.getId());
        return Result.OK(djsAmt);
    }

}
